# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
        ans = head
        cur = head.next
        tmp = ans
        pre = None
        count = 0
        while cur is not None:
            if cur.val != 0:
                count += cur.val
            else:
                tmp.val = count
                count = 0
                pre = tmp
                tmp = tmp.next
            cur = cur.next
        pre.next = None
        return ans